package leetcode.leetcode_222;

import java.util.LinkedList;

class Solution {
    public int countNodes(TreeNode root) {
        if (root == null ){
            return 0;
        }
        LinkedList<TreeNode> nodes = new LinkedList<>();
        int ans =0;
        nodes.add(root);
        while(!nodes.isEmpty()){
            int size = nodes.size();
            ans += size;
            for (int i =0 ;i<size;i++){
                TreeNode poll = nodes.poll();
                if (poll.left != null) {
                    nodes.add(poll.left);
                }
                if (poll.right != null) {
                    nodes.add(poll.right);
                }
            }
        }
        return ans;

    }
}